import Vue from 'vue'
import Vuex from 'vuex'
import addresses from '@/api/addresses'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		store: {},
		cart: [],
		orderType: 'takein',
		address: {},
		addresses: addresses,
		member: {},
		order: {}
	},
	getters: {
		isLogin: state => Object.keys(state.member).length > 0	//是否登录
	},
	mutations: {
		SET_ORDER_TYPE(state, type) {
			state.orderType = type
		},
		SET_MEMBER(state, member) {
			state.member = member
		},
		SET_ADDRESS(state, address) {
			state.address = address
		},
		SET_ADDRESSES(state, addresses) {
			state.addresses = addresses
		},
		SET_STORE(state, store) {
			state.store = store
		},
		SET_CART(state, cart) {
			state.cart = cart
		},
		REMOVE_CART(state) {
			state.cart = []
		},
		SET_ORDER(state, order) {
			state.order = order
		}
	},
	actions: {
		async getStore({commit}) {
			uni.getLocation({
			    type: 'gcj02',
				isHighAccuracy:true,
			    success: function (res) {
					Vue.prototype.$http.post('/about', {longitude:res.longitude,latitude:res.latitude}).then(res => {
						const store = res.data.data
						commit('SET_STORE', store)
					}).catch(err => {
					})
			    }
			});
		},
		async getLogin({commit}){
			Vue.prototype.$http.post('/user', '').then(res => {
				// console.log('000',res)
				if(res.data.code != 401){
					console.log('set_member', res)
					uni.setStorageSync('token', res.data.data.token)
					commit('SET_MEMBER', res.data.data)
				}else{
					console.log('set_member2', res)
					uni.removeStorageSync('token')
					commit('SET_MEMBER', {})
				}
			}).catch(err => {
				if(err.statusCode == 401){
					console.log('set_member3', res)
					uni.removeStorageSync('token')
					commit('SET_MEMBER', '')
				}
			})
		},
		setLogin({dispatch, commit}, e){
			uni.showLoading({
				title: '授权登录中'
			})
			let mobile = uni.getStorageSync('getPhoneNumber')
			if(mobile){
				dispatch('getUserInfo')
			}else{
				if(e.detail.errMsg == 'getPhoneNumber:ok'){
					Vue.prototype.$http.post('/wx/getPhoneNumber', {code:e.detail.code}).then(res => {
						if(res.data.code == 1){
							let phone = res.data.data
							uni.setStorageSync('getPhoneNumber', phone)
							dispatch('getUserInfo')
						}else{
							return uni.showToast({
								icon:'none',
								title:res.data.msg
							})
						}
						
					}).catch(err => {
					
					})
				}
			}
		},
		getUserInfo({commit}){
			uni.login({
				success:(e)=> {
					Vue.prototype.$http.get('wx/login', {params: {code:e.code}}).then(res => {
						let openid = res.data.data.openid
						if(!openid){
							return uni.showToast({
								icon:'none',
								title:'授权失败'
							})
						}else{
							uni.getUserInfo({
								'desc':'同步微信头像和昵称到小程序',
								success:(e)  =>{
									e.userInfo.openid = openid
									let mobile = uni.getStorageSync('getPhoneNumber')
									e.userInfo.mobilePhone = mobile
									// console.log(e.userInfo);
									Vue.prototype.$http.post('/wx/checkLogin', e.userInfo).then(res => {
										let token = res.data.data.token
										let mobilePhone = res.data.data.mobilePhone
										uni.setStorageSync('token',token)
										uni.setStorageSync('getPhoneNumber',mobilePhone)
										uni.hideLoading()
										commit('SET_MEMBER', res.data.data)
										
										if (!mobilePhone) {
											return uni.navigateTo({
												url:"/pages/mine/userinfo"
											})
										}
									}).catch(err => {
									
									})
								},
								fail:function(e){
									return
								}
							})
						}
					}).catch(err => {
					
					})
					// console.log(e)
				}
			})
		}
	}
})

export default store